package cn.iocoder.yudao.module.bpm.service.message;

import cn.iocoder.yudao.framework.common.pojo.wework.WeworkMessageSendReq;
import cn.iocoder.yudao.framework.common.pojo.wework.WeworkMessageSendResponse;
import cn.iocoder.yudao.framework.common.util.string.StrUtils;
import cn.iocoder.yudao.framework.web.config.WebProperties;
import cn.iocoder.yudao.module.bpm.convert.message.BpmMessageConvert;
import cn.iocoder.yudao.module.bpm.enums.message.BpmMessageEnum;
import cn.iocoder.yudao.module.bpm.service.message.dto.BpmMessageSendWhenProcessInstanceApproveReqDTO;
import cn.iocoder.yudao.module.bpm.service.message.dto.BpmMessageSendWhenProcessInstanceRejectReqDTO;
import cn.iocoder.yudao.module.bpm.service.message.dto.BpmMessageSendWhenTaskCreatedReqDTO;
import cn.iocoder.yudao.module.system.api.sms.SmsSendApi;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.system.api.wework.WeworkSmsSendApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * BPM 消息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class BpmMessageServiceImpl implements BpmMessageService {

    @Resource
    private SmsSendApi smsSendApi;

    @Resource
    private WebProperties webProperties;

    @Resource
    private WeworkSmsSendApi weworkSmsSendApi;
    @Resource
    private AdminUserApi adminUserApi;

    /**
     * 企业微信通知分配人消息模板
     */
    private static final String WEWORK_NOTIFY_TEMPLATE = "您收到了一条新的待办任务：%s-%s，申请人：%s，处理链接：%s";
    /**
     * 任务发起人提醒卡片消息模板
     */
    private static final String WEWORK_NOTIFY_CARD_TEMPLATE = "<div class=\"gray\">流程名称  </div><span class=\"normal\">%s</span> <div class=\"gray\">节点名称  </div><span class=\"gray\">%s</span><div class=\"gray\">申  请  人  </div><span class=\"highlight\">%s</span>";


    @Override
    public void sendMessageWhenProcessInstanceApprove(BpmMessageSendWhenProcessInstanceApproveReqDTO reqDTO) {
        Map<String, Object> templateParams = new HashMap<>();
        templateParams.put("processInstanceName", reqDTO.getProcessInstanceName());
        templateParams.put("detailUrl", getProcessInstanceDetailUrl(reqDTO.getProcessInstanceId()));
        smsSendApi.sendSingleSmsToAdmin(BpmMessageConvert.INSTANCE.convert(reqDTO.getStartUserId(),
                BpmMessageEnum.PROCESS_INSTANCE_APPROVE.getSmsTemplateCode(), templateParams));
    }

    @Override
    public void sendMessageWhenProcessInstanceReject(BpmMessageSendWhenProcessInstanceRejectReqDTO reqDTO) {
        Map<String, Object> templateParams = new HashMap<>();
        templateParams.put("processInstanceName", reqDTO.getProcessInstanceName());
        templateParams.put("reason", reqDTO.getReason());
        templateParams.put("detailUrl", getProcessInstanceDetailUrl(reqDTO.getProcessInstanceId()));
        smsSendApi.sendSingleSmsToAdmin(BpmMessageConvert.INSTANCE.convert(reqDTO.getStartUserId(),
                BpmMessageEnum.PROCESS_INSTANCE_REJECT.getSmsTemplateCode(), templateParams));
    }

    @Override
    public void sendMessageWhenTaskAssigned(BpmMessageSendWhenTaskCreatedReqDTO reqDTO) {
        //1.发送短信
        Map<String, Object> templateParams = new HashMap<>();
        templateParams.put("processInstanceName", reqDTO.getProcessInstanceName());
        templateParams.put("taskName", reqDTO.getTaskName());
        templateParams.put("startUserNickname", reqDTO.getStartUserNickname());
        templateParams.put("detailUrl", getProcessInstanceDetailUrl(reqDTO.getProcessInstanceId()));
        smsSendApi.sendSingleSmsToAdmin(BpmMessageConvert.INSTANCE.convert(reqDTO.getAssigneeUserId(),
                BpmMessageEnum.TASK_ASSIGNED.getSmsTemplateCode(), templateParams));

        //2.发送企业微信消息给任务审批人
        WeworkMessageSendReq req = new WeworkMessageSendReq();
        //2.1 获取用户账户
        AdminUserRespDTO user = adminUserApi.getUser(reqDTO.getAssigneeUserId());
        //2.2 构建请求参数
        req.setTouser(user.getUsername());
//        req.setMsgtype("text");
//        String content = StrUtils.replaceSpecialStr(WEWORK_NOTIFY_TEMPLATE, reqDTO.getProcessInstanceName(),
//                reqDTO.getTaskName(), reqDTO.getStartUserNickname(), getProcessInstanceDetailUrl(reqDTO.getProcessInstanceId()));
//        req.setText(new WeworkMessageSendReq.TextObject().setContent(content));

        req.setMsgtype("textcard");
        WeworkMessageSendReq.TextCardObject card = new WeworkMessageSendReq.TextCardObject();
        card.setTitle("您有新的任务待处理");
        String description = StrUtils.replaceSpecialStr(WEWORK_NOTIFY_CARD_TEMPLATE, reqDTO.getProcessInstanceName(), reqDTO.getTaskName(), user.getNickname());
        card.setDescription(description);
        card.setUrl(getProcessInstanceDetailUrl(reqDTO.getProcessInstanceId()));
        req.setTextcard(card);
        //2.3 发送企业微信消息给审批人
        weworkSmsSendApi.sendSingleSmsToUser(req);
    }

    private String getProcessInstanceDetailUrl(String taskId) {
        return webProperties.getAdminUi().getUrl() + "/bpm/process-instance/detail?id=" + taskId;
    }

}
